// Purpose : This page is use for add topic detail and view all questions.
// Date    : 30 August, 2007 
// Author  : Vinayak Singh Rathore

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
using SiteManager;
public partial class Community_Forum : System.Web.UI.Page
{
    Manager objManger = null;
    protected string TopicName="";    
    protected string UserName;
    int userType = 1;
    bool TopicID = false;
    bool SupportGroupTopicId =false;
    bool backpresstopicid = false;    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["TopicId"] = "";
            //if user is not authenticated the Post message box vissible false
            //COMMENTED AT 22/02/2008 BY RANJEET
            //if (Session["userID"] == null || Session["userID"].ToString().Length <= 0)
            //{
                
            //    PostMessage.Visible = false;                                           
            //}
            //END
            //ADDED AT 22/02/2008 BY RANJEET 
            if (Request.QueryString["SupportGroupTopicId"] == null)
            {
                SupportGroupTopicId = true;
            }            
            if (Request.QueryString["TopicName"] == null)
            {
                TopicID = true;
            }            
            if (Request.QueryString["ID"] == null)
            {
                backpresstopicid = true;
            }
            if (SupportGroupTopicId && TopicID && backpresstopicid)
            {
                Response.Redirect(Session["strHttp"] + "Community/community.aspx");
            }
            SetTopicID();
            //END
            bindRepeater();
            //btnSubmit.Attributes.Add("OnClick", "return CheckForumMessageIsBlank('" + txtDescription.ClientID + "','" + lblMessage.ClientID + "');");
        }
        SetTopicName();  
        bindRepeater();
        btnSubmit.Attributes.Add("OnClick", "return CheckForumMessageIsBlank('" + txtDescription.ClientID + "','" + lblMessage.ClientID + "');");        
        
    }
    /// <summary>
    /// Handler for submmit button
    /// </summary>
    /// <author>Vinayak Singh Rathore</author>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        //ADDED BY RANJEET AT 22/02/2008 IF ANYONE POST TOPIC WITHOUT LOGIN THEN REDIRECT LOGIN PAGE.
        if (Session["userID"] == null || Session["userID"].ToString().Length <= 0)
        {
            lblMessage.Text = "You can not post message without login";
            return;
        } 
        //END
        StringBuilder sb = new StringBuilder();
       objManger = new Manager(true,"ConnectionString");

       if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]) == "Doctor")))
           userType = 2;
       else if (Session["userType"].ToString() == "SiteUser")
           userType = 3;
       else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
           userType = 4;

       if (Session["TopicId"].ToString() !=""  && Session["userID"] != null && Session["userID"].ToString().Length > 0)
        {
            SqlParameter[] objParameter = new SqlParameter[6];
            objParameter[0] = new SqlParameter("@TopicID", SqlDbType.BigInt);
            objParameter[0].Value = Convert.ToInt32(Session["TopicId"]);
            objParameter[1] = new SqlParameter("@ParentID", SqlDbType.BigInt);
            objParameter[1].Value = 0;
            objParameter[2] = new SqlParameter("@AuthorID", SqlDbType.BigInt);
            objParameter[2].Value = Convert.ToInt32(Session["userID"]);
            objParameter[3] = new SqlParameter("@AuthorType", SqlDbType.BigInt);
            objParameter[3].Value = userType;
            objParameter[4] = new SqlParameter("@Msg", SqlDbType.Text);
            objParameter[4].Value = txtDescription.Text;
            objParameter[5] = new SqlParameter("@IsError", SqlDbType.Bit);
            objParameter[5].Direction = ParameterDirection.Output;
            objManger = new Manager(true,"ConnectionString");
            objManger.ExecuteQuery("AddForumandRespose", objParameter);
            if (Convert.ToBoolean(objParameter[5].Value))
            {
                lblMessage.Text = "You are not valid user.";
            }
            else
            {
                lblMessage.Text = "Message has been Posted ";
                txtDescription.Text = "";               
            }
            objManger = null;
        
        }
        else
        {
            
            lblMessage.Text = "Login for insert Question";
        }
        objManger = null;        
        bindRepeater();
    }

    public void bindRepeater()
    {
        try
        {
            if (Convert.ToBoolean(Convert.ToInt32(Session["TopicId"])) == false)
            {
                Response.Redirect(Session["strHttp"] + "Community/community.aspx");
            }
        }
        catch
        {
            Response.Redirect(Session["strHttp"] + "Community/community.aspx");
        }
        SqlParameter[] objParameter = new SqlParameter[2];
        objParameter[0] = new SqlParameter("@TopicID", SqlDbType.BigInt);
        objParameter[0].Value = Convert.ToInt32(Session["TopicId"]);
        objParameter[1] = new SqlParameter("@Search",SqlDbType.VarChar,100);
        objParameter[1].Value = txtSearch.Text.ToString();
        pagingForum.SqlParameterList= objParameter;
        
    }

    /// <summary>
    /// This function return the topic name
    /// <author>Vinayak Singh Rathore</author>
    /// <date>24 Sep, 2007</date>
    /// </summary>
    public void SetTopicName()
    {
        objManger = new Manager(true,"ConnectionString");
        SqlDataReader objDataReader = null;
        objDataReader = objManger.ExecuteQuery("select name from tbl_topics where id=" + Convert.ToInt32(Session["TopicId"]));
        if (objDataReader.HasRows)
        {
            objDataReader.Read();
            TopicName = objDataReader["name"].ToString();
        }
        objManger = null;
           
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        txtDescription.Text = "";
        SqlParameter[] objParameter = new SqlParameter[2];
        objParameter[0] = new SqlParameter("@TopicID", SqlDbType.BigInt);
        objParameter[0].Value = Convert.ToInt32(Session["TopicId"]);
        objParameter[1] = new SqlParameter("@Search", SqlDbType.VarChar, 100);
        objParameter[1].Value = txtSearch.Text.ToString();
        pagingForum.SqlParameterList = objParameter;
    }

    public void SetTopicID()
    {
        objManger = new Manager(true,"ConnectionString");
        SqlDataReader objDataReader = null;
        if (Request.QueryString["SupportGroupTopicId"] != null)
            Session["TopicId"] = Convert.ToInt32(Request.QueryString["SupportGroupTopicId"]);
        else if (Request.QueryString["TopicName"] != null)
        {
            string TopicName = Request.QueryString["TopicName"].ToString().Replace("_", " ");
            objDataReader = objManger.ExecuteQuery("select id from tbl_topics where Name ='" + TopicName.ToString().Trim() + "'");
            if (objDataReader.HasRows)
            {
                objDataReader.Read();
                Session["TopicId"] = "";
                Session["TopicId"] = Convert.ToInt32(objDataReader["id"].ToString());
            }
        }
        else if (Request.QueryString["ID"] != null)
        {
            Session["TopicId"] = Convert.ToInt32(Request.QueryString["ID"]);
        }

        objManger = null;

    }   
}
